package com.yiqi.zhiyuan.feature.login.presenter;

import android.text.TextUtils;

import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.ValidatorUtil;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.dto.RegisterDTO;
import com.yiqi.zhiyuan.entity.enums.VCodeType;
import com.yiqi.zhiyuan.feature.login.contract.ResetContract;
import com.yiqi.zhiyuan.feature.login.dependencies.reset.DaggerResetPresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @ProjectName: ZhiYuan
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/3/13 14:03
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/13 14:03
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ResetPresenter implements ResetContract.Presenter {

    private ResetContract.View mView;

    @Inject
    ResetContract.Model mModel;


    private Disposable mDisposable;

    public ResetPresenter(ResetContract.View mView) {
        this.mView = mView;
        DaggerResetPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void sendCode(String mobile,int type) {
        if (TextUtils.isEmpty(mobile) || !ValidatorUtil.isMobile(mobile)) {
            mView.showGetCodeError(AM.res().getString(R.string.toast_phone_error));
            return;
        }

        mModel.doSendCode(mobile, type)
                .compose(mView.<String>bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showGetCodeSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showGetCodeError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void resetPwd(String mobile, String password, String vcode) {
        if (TextUtils.isEmpty(mobile) || !ValidatorUtil.isMobile(mobile)) {
            mView.showResetPwdError(AM.res().getString(R.string.toast_phone_error));
            return;
        }
        if (TextUtils.isEmpty(vcode)) {
            mView.showResetPwdError(AM.res().getString(R.string.toast_code_input));
            return;
        }

        if (TextUtils.isEmpty(password)) {
            mView.showResetPwdError(AM.res().getString(R.string.toast_password_input));
            return;
        }
        if (password.length() < 6) {
            mView.showResetPwdError(AM.res().getString(R.string.toast_password_6));
            return;
        }

        RegisterDTO registerDTO=   new RegisterDTO(mobile,password,vcode);

        mModel.doResetPwd(registerDTO)
                .compose(mView.<UserInfoBo>bindToLife())
                .subscribe(new Observer<UserInfoBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(UserInfoBo s) {
                        mView.dismissLoading();
                        loginSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showResetPwdError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });

    }

    @Override
    public void editPhone(String mobile, String vcode,Integer flag) {
        if (TextUtils.isEmpty(mobile) || !ValidatorUtil.isMobile(mobile)) {
            mView.showResetPwdError(AM.res().getString(R.string.toast_phone_error));
            return;
        }
        if (TextUtils.isEmpty(vcode)) {
            mView.showResetPwdError(AM.res().getString(R.string.toast_code_input));
            return;
        }


        RegisterDTO registerDTO=   new RegisterDTO(mobile,vcode);
        registerDTO.setFlag(flag);

        mModel.doEditPhone(registerDTO)
                .compose(mView.<UserInfoBo>bindToLife())
                .subscribe(new Observer<UserInfoBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(UserInfoBo s) {
                        mView.dismissLoading();
                      mView.showResetPwdSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showResetPwdError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    private void loginSuccess() {
        mView.showResetPwdSuccess();
    }


}